package com.example.hfiveandaplication.view;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.barradata.R;
import com.example.hfiveandaplication.common.DialogManager;


public class ShootingDialog {
    private AlertDialog dialog;
    private CallBack callBack;

    @SuppressLint("NewApi")
    public ShootingDialog(final Context context, String title) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = View.inflate(context, R.layout.idcard_hint_diaglog, null);

        TextView tvTitle = view.findViewById(R.id.title_tv);
        TextView confirm = view.findViewById(R.id.next_button);
        CheckBox checkBox = view.findViewById(R.id.checkbox);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                confirm.setAlpha(1.0f);
                confirm.setEnabled(true);
            } else {
                confirm.setAlpha(0.55f);
                confirm.setEnabled(false);
            }
        });
        checkBox.setChecked(true);
        tvTitle.setText(title);
        builder.setView(view);
        dialog = builder.create();
        dialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent);
        // 不允许取消
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);


        confirm.setOnClickListener(v -> {
            if (callBack != null) {
                callBack.onConfirm();
            }
        });

    }

    public void show() {
        DialogManager.getManager().showDialogStack(dialog);
    }

    public void hide() {
        DialogManager.getManager().dismissDialog(dialog);
    }

    public void setOnClickListener(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void onConfirm();
    }

}
